package com.ddyx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.Activity;
import com.ddyx.entity.ActivityGoods;
import com.ddyx.mapper.ActivityGoodsMapper;
import com.ddyx.mapper.ActivityMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.service.ActivityService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;



@Slf4j
@Service
public class ActivityServiceImpl  implements ActivityService {
	
	@Resource
	ActivityMapper activityMapper;
	
	@Resource
	ActivityGoodsMapper activityGoodsMapper;
	@Resource
	GoodsMapper goodsMapper;
	
	public ResponseForm getActivity(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			
			List<Map<String, Object>> list = activityMapper.getActivityByType(paramMap);
			
			
			
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	@Transactional
	@Override
	public ResponseForm updateActivity(RequestForm param) {
		
		ResponseForm result = new ResponseForm();
		if (null != param) {
			Activity bean = new Activity();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				MyBeanUtils.populate(bean, paramMap);
				
				//List<String> goodsList = (List) paramMap.get("link");
				List<String> goodsList = (List) paramMap.get("goodsList");
				if(goodsList.size()==0){
					result.setStatus(false);
					result.setMessage("商品不能为空");
					return result;
				}
				
				if(bean.getType()!=1){
					if(goodsList.size()==1){
						bean.setLink(String.valueOf(goodsList.get(0)));
					}else{
						bean.setLink("0");
					}
				}else{
					bean.setLink("1");
				}
				bean.setCreateTime(new Date());
				activityMapper.updateByPrimaryKeySelective(bean);
				//活动与商品对应
				if(bean.getType()!=1&&goodsList.size()>1 ){
					Example example = new Example(ActivityGoods.class);
					Criteria c = example.createCriteria();
					c.andEqualTo("activityId",bean.getId() );
					activityGoodsMapper.deleteByExample(example);
					ActivityGoods agBean;
					for(int i =0;i<goodsList.size();i++){
						agBean=new ActivityGoods();
						agBean.setActivityId(bean.getId());
						agBean.setGoodsId(Integer.valueOf(goodsList.get(i)));
						activityGoodsMapper.insert(agBean);
					}
				}
				
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}
	@Transactional
	@Override
	public ResponseForm addActivity(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			Activity bean = new Activity();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				MyBeanUtils.populate(bean, paramMap);
				
				List<String> goodsList = (List) paramMap.get("goodsList");
				if(goodsList.size()==0){
					result.setStatus(false);
					result.setMessage("商品不能为空");
					return result;
				}
				
				// link未处理
				if(bean.getType()!=1){
					if(goodsList.size()==1){
						bean.setLink(String.valueOf(goodsList.get(0)));
					}else{
						bean.setLink("0");
					}
				}else{
					bean.setLink("1");
				}
				bean.setStatus(0);
				bean.setCreateTime(new Date());
				
				activityMapper.insertSelective(bean);
				
				//活动与商品对应
				if(bean.getType()!=1&&goodsList.size()>1 ){
					ActivityGoods agBean;
					for(int i =0;i<goodsList.size();i++){
						agBean=new ActivityGoods();
						agBean.setActivityId(bean.getId());
						agBean.setGoodsId(Integer.valueOf(goodsList.get(i)));
						activityGoodsMapper.insert(agBean);
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getActivityById(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			Activity bean = new Activity();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				MyBeanUtils.populate(bean, paramMap);
				
				bean = activityMapper.selectOne(bean);
				
					if(bean.getType()!=1){
						boolean b=bean.getLink().startsWith("http");
						if(!b){
							if(!bean.getLink().equals("0")){
								List list = new ArrayList();
								list.add(bean.getLink());
								bean.setGoodsList(list);
							}else{
								List aList =activityMapper.getGoodsIdByActivityId(bean.getId());
								bean.setGoodsList(aList);
							}
							
							
						}
					}
				
				
				
				
				result.setData(bean);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	@Override
	public ResponseForm addActivityCheck(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				
				Example example = new Example(Activity.class);
				Criteria c = example.createCriteria();
				c.andEqualTo("type", paramMap.get("type"));
				c.andEqualTo("storeNo", paramMap.get("storeNo"));
				int count = activityMapper.selectCountByExample(example);
				result.setData(count);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

}
